package in.muton.edushare.pages.common;

import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.curricula.NewCurriculaPage;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EntityListing;
import in.muton.panels.paginator.badgehelpers.course.ProgramBadgeInterface;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CurriculumDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public class CurriculaPanel extends EdusharePanel {

	public CurriculaPanel(String id, final IModel<?> model,
			EduShareBasePage page) {
		super(id, model, page);

		ProgramBadgeInterface pbif;
		if (model.getObject() instanceof Departments)
			pbif = new ProgramBadgeInterface((Departments) model.getObject());
		else
			pbif = new ProgramBadgeInterface((Universities) model.getObject());

		add(new EntityListing<Curricula>("programsLV",
				new InnerList<Curricula>() {
					private static final long serialVersionUID = 1L;

					@Override
					public List<IModel<Curricula>> getObject() {
						return getList(model.getObject()).getObject();
					}
				}, pbif, page));

		Form<Void> f = new Form<Void>("form");
		add(f);
		f.add(new Button("button") {
			@Override
			public void onSubmit() {
				PageParameters params = new PageParameters();
				if (model.getObject() instanceof Departments) {
					Departments d = (Departments) model.getObject();
					params.put("0", new DepartmentsDAO(getORMInterface())
							.getParent(d).getUniqueName());
					params.put("1", "0");
					params.put("2", Integer.toString(d.getDepartmentId()));
				} else {
					Universities u = (Universities) model.getObject();
					params.put("0", u.getUniqueName());
				}
				setResponsePage(NewCurriculaPage.class, params);
			}

			@Override
			public boolean isVisible() {
				if (model.getObject() instanceof Departments) {
					DeptRoleCheck drc = new DeptRoleCheck((Departments) model
							.getObject(), getLoggedInUser());
					if (drc.isUnivAdmin())
						return true;
				} else {
					UnivRoleCheck urc = new UnivRoleCheck((Universities) model
							.getObject(), getLoggedInUser());
					if (urc.isAdmin())
						return true;
				}
				return false;
			}

			private static final long serialVersionUID = -1853817260280015167L;
		});
	}

	private CayenneList<Curricula> getList(Object obj) {
		CurriculumDAO cDAO = new CurriculumDAO(getORMInterface());
		CayenneList<Curricula> currList = null;
		if (obj instanceof Universities) {
			currList = new CayenneList<Curricula>(cDAO
					.getCurricula((Universities) obj));
		} else if (obj instanceof Departments) {
			currList = new CayenneList<Curricula>(cDAO
					.getCurriculaByDept((Departments) obj));
		} else if (obj instanceof Courses) {
			currList = new CayenneList<Curricula>(cDAO
					.getCurrByCourse((Courses) obj));
		}
		return currList;
	}

	private static final long serialVersionUID = 2981492545197169515L;
}
